package uw.padres.client;

import java.io.BufferedReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import ca.utoronto.msrg.padres.client.*;
import ca.utoronto.msrg.padres.common.message.Advertisement;
import ca.utoronto.msrg.padres.common.message.Publication;
import ca.utoronto.msrg.padres.common.util.CommandLine;
import ca.utoronto.msrg.padres.common.util.LogException;
import ca.utoronto.msrg.padres.common.util.LogSetup;

public class UWClient extends Client{
	
	// CONSTANTS
		private static final String DEFAULT_ADVERTISEMENT = "[class,eq,'STOCK'],[symbol,isPresent,'A'],[open,isPresent,1],"
				+ "[high,isPresent,1],[low,isPresent,1],[close,isPresent,1],"
				+ "[volume,isPresent,1],[date,isPresent,'A']";

		private final static String DEFAULT_CLIENT_ID = new SimpleDateFormat("MMdd_HH.mm.ss").format(new Date());//getCurrentDateTime();
		private final static String DEFAULT_PUBLISHER_ID = "P" + DEFAULT_CLIENT_ID;
		private final static String DEFAULT_PUB_RATE = "1.0"; // msg/min
		private final static String DEFAULT_PUB_DATA_PATH = ClientConfig.PADRES_HOME
				+ "/demo/data/stockquotes/";
		
		private final static String DEFAULT_PUB_DELAY = "0";
		private final static long DELAY_BETWEEN_NEW_PUBLISHER = 100; // ms

		// Command-line argument-related constants
		private static final String CMD_ARG_SYMBOL = "s";
		private static final String CMD_ARG_RATE = "r";
		private static final String CMD_ARG_PUB_DELAY = "d";

		// make sure .split() uses "\\" for special chars
		public static final String PUB_DELIMIT_CHAR = "/";

		// The stock that this publisher publishes
		private  String symbol;

		// Time in between publishing in milliseconds.
		private  long publishPeriod;

		private  double rate; // just for reference when logging

		// time to start publish after instantiation in ms
		private  long pubDelay;

		// Advertisement for this publisher. This doesn't change after it is initialized
		private  Advertisement advertisement;

		// Useful when we want to disconnect from a specified broker and when we want to send a
		// publication to all of our current connected brokers
		private  HashMap<String, String> brokerIdAdvIdMap;

		// Used for reading in publications to publish
		private BufferedReader reader;

		// The number of publication sent
		private long pubCount;

		// The number of times the entire publication file was published
		private int publicationRounds;

		private Publication lastPublished = null;

		// to read from a file or just to repeat one publication again and again
		// it is a hack for profiling experiments; keep it "false" for normal StockPublisher functions
		private boolean repeatPub = false;
	

	public UWClient() throws ClientException {
		super();
	}
	

	public static void main(String[] args) {
		try {
			new LogSetup(null);
		} catch (LogException e) {
			e.printStackTrace();
			System.exit(1);
		}
		
		String[] cliKeys = ClientConfig.getCommandLineKeys();
		String[] fullCLIKeys = new String[cliKeys.length + 3];
		System.arraycopy(cliKeys, 0, fullCLIKeys, 0, cliKeys.length);
		fullCLIKeys[cliKeys.length] = CMD_ARG_SYMBOL + ":";
		fullCLIKeys[cliKeys.length + 1] = CMD_ARG_RATE + ":";
		fullCLIKeys[cliKeys.length + 2] = CMD_ARG_PUB_DELAY + ":";
		CommandLine cmdLine = new CommandLine(fullCLIKeys);
		try {
			cmdLine.processCommandLine(args);
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
		String rateString = cmdLine.getOptionValue(CMD_ARG_RATE, DEFAULT_PUB_RATE);
		String stockString = cmdLine.getOptionValue(CMD_ARG_SYMBOL);
		String pubDelay = cmdLine.getOptionValue(CMD_ARG_PUB_DELAY, DEFAULT_PUB_DELAY);
	}
	
	
}
